﻿<%@ Page Language="C#" MasterPageFile="~/_layouts/application.master" AutoEventWireup="true"
    EnableViewState="true" %>

<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Import Namespace="Microsoft.SharePoint.WebControls" %>
<%@ Import Namespace="Microsoft.SharePoint.Workflow" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Xml" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
    Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
    <table class="ms-formtable" border="0" cellpadding="0" cellspacing="0" width="100%">
        <tr>
            <td colspan="2" valign="top" class="ms-formlabel">
                <asp:Label ID="lblError" runat="server" />
            </td>
        </tr>
        <tr>
            <td valign="top" style="width: 50%" class="ms-formlabel">
                <asp:TreeView ID="treFiles" runat="server" NodeStyle-CssClass="ms-formlabel" NodeStyle-Width="100%" />
            </td>
        </tr>
        <tr>
            <td>
                <asp:Button ID="btnApply" runat="server" Text="Uncustomize checked files" OnClick="btnApply_Click" />
            </td>
            <td>
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;
            </td>
        </tr>
        <tr>
            <td colspan="2" class="ms-formlabel">
                Created by <a href="http://blog.sharepoint.ch">David Schneider</a>.
            </td>
        </tr>
    </table>
</asp:Content>
<asp:Content ID="BreadCrumb" ContentPlaceHolderID="PlaceHolderTitleBreadcrumb" runat="server">
    <asp:HyperLink ID="hlBack" runat="server" />
</asp:Content>
<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea"
    runat="server">
    <asp:Label ID="lbPageTitle" runat="server" Text="Uncustomizer" />
</asp:Content>
<asp:Content ID="TopMenu" ContentPlaceHolderID="PlaceHolderPageDescription" runat="server">
    <asp:Label ID="lbPageDescription" runat="server" Text="" />
</asp:Content>

<script runat="server">
    protected override void OnLoad(EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            TreeNode rootNode = new TreeNode("Root");
            treFiles.Nodes.Add(rootNode);
            AddNodes(rootNode, SPContext.Current.Site.RootWeb);

        }
    }

    #region HelperMethods
    private void AddNodes(TreeNode pParentNode, SPWeb pParentSite)
    {
        foreach (SPWeb site in pParentSite.Webs)
        {
            TreeNode node = new TreeNode();
            node.Text = string.Format("{0}", site.Title);
            node.Value = site.ID.ToString();
            node.ShowCheckBox = false;

            AddNodes(node, site);
            AddLists(node, site);

            pParentNode.ChildNodes.Add(node);
            site.Dispose();
        }
    }

    private void AddLists(TreeNode pParentNode, SPWeb pParentSite)
    {
        foreach (SPList list in pParentSite.Lists)
        {
            AddFolder(pParentNode, list.RootFolder);
        }
    }

    private void AddFolder(TreeNode pParentNode, SPFolder pParentFolder)
    {

        foreach (SPFolder folder in pParentFolder.SubFolders)
        {
            AddFolder(pParentNode, folder);
        }

        foreach (SPFile file in pParentFolder.Files)
        {
            if (file.CustomizedPageStatus == SPCustomizedPageStatus.Customized)
            {
                TreeNode node = new TreeNode();
                node.Text = string.Format("{0}", file.ServerRelativeUrl);
                node.Value = string.Format("{0}${1}", pParentFolder.ParentWeb.ID.ToString(), file.UniqueId.ToString());
                node.ShowCheckBox = true;
                pParentNode.ChildNodes.Add(node);
            }
        }
    }
    #endregion

    #region Events
    private void btnApply_Click(object sender, EventArgs e)
    {
        int cnt = 0;
        foreach (TreeNode node in treFiles.Nodes)
        {
            cnt += ApplyApprovalSettings(node);
        }

        lblError.Text = string.Format("{0} sites updated!", cnt);
        lblError.Style.Add(HtmlTextWriterStyle.Color, "Green");
    }

    private int ApplyApprovalSettings(TreeNode node)
    {
        int cnt = 0;
        if (node.Checked)
        {
            string[] keys = node.Value.Split('$');
            Guid siteID = new Guid(keys[0]);
            Guid itemID = new Guid(keys[1]);
            using (SPWeb site = SPContext.Current.Site.OpenWeb(siteID))
            {
                SPFile file = site.GetFile(itemID);

                file.RevertContentStream();
                cnt++;
            }
        }

        foreach (TreeNode subNode in node.ChildNodes)
        {
            cnt += ApplyApprovalSettings(subNode);
        }
        return cnt;
    }

    private struct ItemIDs
    {
        public Guid WebID;
        public Guid ItemID;
    }
    #endregion
</script>

